﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="inc_SanPhamLienQuan.ascx.cs" Inherits="AdminPortal_Controls_SanPham_inc_SanPhamLienQuan" %>
<%--<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>--%>
<script type="text/javascript">
    $(document).ready(function () {
        //get list san pham mua kem 
        var cmdhidpID = $('[id$=hidIDproduct]');
        var CurrentProID = $('[id$=hidpID]').val();
        var urlListSPMK = '<%=Page.ResolveUrl("~/AdminPortal/Controls/SanPham/Ajax/Product_ListSanPhamMuaKem.ashx?id=") %>' + CurrentProID;
        $.getJSON(urlListSPMK,{}, function (data) {
            $('[id$=listProductChoice] option').remove();
            $.each(data, function (i, obj) {
                $('[id$=listProductChoice]').append('<option value="' + obj['ID'] + '">' + obj['TenSanPham'] + '</option>');
                //add san pham vao list
                $(cmdhidpID).val(AddproductToList(obj['ID'], $(cmdhidpID).val()));
            });
        });

        //load category product on dropdownlist
        var urlcateproduct = '<%=Page.ResolveUrl("~/AdminPortal/Controls/SanPham/Ajax/CategoryProduct_ToListbox.ashx") %>';
        $.getJSON(urlcateproduct, function (data) {
            $('[id$=ddlCateParent] option').remove();
            //them Item dau tien cho dropdown
            $('[id$=ddlCateParent]').append($('<option selected="selected" value="0">--</option>'));
            $.each(data, function (i, obj) {
                $('[id$=ddlCateParent]').append($('<option value="' + obj['ID'] + '">' + obj['TenDanhMuc'] + '</option>'));
            });
        });
    });

    function BindProductToList(_name) {
        var CateID = $('#' + _name).val();
        var urlList = '<%=Page.ResolveUrl("~/AdminPortal/Controls/SanPham/Ajax/Product_ToListBox.ashx?id=") %>' + CateID;
        //alert(CateID);
        $("[id$=listProduct] option").remove();
        $.getJSON(urlList, function (data) {
            $.each(data, function (i, obj) {
                $("[id$=listProduct]").append('<option value="' + obj['ID'] + '">' + obj['TenSanPham'] + '</option>');
            });
        });
    }
    /*
        Them va xoa san pham lien quan
    */

    //double click get product
    function getProductToListbox() {
        var cmdListProduct = $('[id$=listProduct] option:selected');
        var cmdListChoice = $('[id$=listProductChoice]');
        var cmdhidpID = $('[id$=hidIDproduct]');
        var pID = cmdListProduct.val();
        var pName = cmdListProduct.text();

        if (pID != '' && pName != '') {

            var opt = $('[id$=listProductChoice] option[value=' + pID + ']').val();
            if (opt == undefined) {
                cmdListChoice.append('<option value="' + pID + '">' + pName + '</option>');
                // add product to list
                $(cmdhidpID).val(AddproductToList(pID, $(cmdhidpID).val()));
            }
        }
    }

    //-- button chon nhieu san pham sang
    function btnAddProduct() {
        var cmdListProduct = $('[id$=listProduct] option:selected');
        var cmdListChoice = $('[id$=listProductChoice]');
        var cmdhidpID = $('[id$=hidIDproduct]');
        $(cmdListProduct).each(function () {
            var pID = $(this).val();
            var pName = $(this).text();
            if (pID != '' || pID != null) {
                var opt = $('[id$=listProductChoice] option[value=' + pID + ']').val();
                if (opt == undefined) {
                    cmdListChoice.append('<option value="' + pID + '">' + pName + '</option>');
                    //add product to list
                    $(cmdhidpID).val(AddproductToList(pID, cmdhidpID.val()));                  
                }
            }
        });
        
    }

    //button xoa cac san pham
    function btnDelProduct() {
        var cmdListChoice = $('[id$=listProductChoice] option:selected');
        var cmdhidpID = $('[id$=hidIDproduct]');
        $(cmdListChoice).each(function () {
            var pID = $(this).val();
            $(this).remove();
            //neu ID co trong list thi xoa no di    
            $('[id$=hidIDproduct]').val(RemoveProductFromList(pID, $(cmdhidpID).val())); 
        });
             
    }

    // double click xoa san pham
    function dblDelProduct() {
        var cmdListChoice = $('[id$=listProductChoice] option:selected');
        var cmdhidpID = $('[id$=hidIDproduct]');
        var pID = $(cmdListChoice).val();
        $(cmdListChoice).remove();
        //neu ID co trong list thi xoa no di     
        $(cmdhidpID).val( RemoveProductFromList(pID, $(cmdhidpID).val()));
    }

    // SELECT toan bo sanpham
    function SelectAllproduct() {
        var cmdListProduct = $('[id$=listProduct] option');
        var cmdListChoice = $('[id$=listProductChoice]');
        var cmdhidpID = $('[id$=hidIDproduct]');
        $(cmdListProduct).each(function () {
            var pID = $(this).val();
            var pName = $(this).text();
            var opt = $('[id$=listProductChoice] option[value=' + pID + ']').val();
            if (opt == undefined) {
                cmdListChoice.append('<option value="' + pID + '">' + pName + '</option>');
                //add product
                $(cmdhidpID).val(AddproductToList(pID, $(cmdhidpID).val()));
            }
        });
        
    }

    //DELETE ALL product
    function DeleteAllProduct() {
        var cmdListChoice = $('[id$=listProductChoice] option');
        var cmdhidpID = $('[id$=hidIDproduct]');
        $(cmdListChoice).remove()
//        $(cmdListChoice).each(function () {
//            var pID = $(this).val();
//            $(this).remove();
//            ListPID = RemoveProductFromList(pID, ListPID);
//        });
        $(cmdhidpID).val('');
    }

    //-------------------------------------------------------------------------
    function AddproductToList(pID, _ListID) {
        if (_ListID == '' || _ListID==null) {
            //neu list dang null thi add lan dau tien
            _ListID = pID;
        } else {
            //cac lan tiep theo phan cach nhau bang dau , de sau nay split
            _ListID += "," + pID;
        }
        return _ListID;
    }

    function RemoveProductFromList(pID, _ListID) {
        if (_ListID != '' || _ListID==null) {
            if (_ListID.indexOf(pID) > -1) {
                _ListID = _ListID.replace(',' + pID, '');
                _ListID = _ListID.replace(pID, '');
                _ListID = _ListID.replace(',,', ',');
                if (_ListID.indexOf(',') == 0)
                    _ListID = _ListID.substring(1);
            }
        }
        return _ListID;
    }
//-----------------------------------------------------------------------
    function btnTest() {
        alert($('[id$=hidIDproduct]').val());
        return false;
    }
</script>
<%--<asp:UpdatePanel runat="server" ID="upPanl">
    <ContentTemplate>--%>
         <table width="100%" class="tableEditInfo">
            <tbody>
                <tr>
                    <td style="width: 130px;" class="itemTableLeft">
                        Danh mục Sản phẩm
                    </td>
                    <td class="itemTableRight">
                        <select id="ddlCateParent" onchange="javascript:BindProductToList('ddlCateParent');"></select>
                       <%-- <asp:DropDownList ID="ddlCateParent" runat="server" AutoPostBack="True" 
                            onselectedindexchanged="ddlCateParent_SelectedIndexChanged" >
                        </asp:DropDownList>--%>
                    </td>
                </tr>
                <tr>
                    <td class="itemTableLeft">
                        Thêm sản phẩm liên quan
                    </td>
                    <td class="itemTableRight">
                        <table cellpadding="0" cellspacing="0" style="border:none;">
                            <tr>
                                <td align="left" style="border:none;width:250px;">Các sản phẩm</td>
                                <td style="border:none;"></td>
                                <td align="left" style="border:none;"> 
                                <ul class="AddProTogether">
                                    <li><a id="OptioncheckAll" href="" onclick="javascript:SelectAllproduct();return false;">Chọn hết</a>&nbsp;</li>
                                    <li><img src="../images/icon/redo.png" alt="" width="16" height="16" /></li>   
                                    <li>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;</li>
                                    <li><a id="OptionUncheck" href="" onclick="javascript:DeleteAllProduct();return false;">Xoá hết</a>&nbsp;</li>
                                    <li><img src="../images/icon/undo.png" align="left" alt="" width="16" height="16" /></li>
                                </ul>
                                </td>
                            </tr>
                            <tr>
                                <td valign="top" align="left" style="border:none;">
                                    <select id="listProduct" multiple="multiple" style="width:250px;height:200px;" ondblclick="javascript:getProductToListbox();"></select>
                                    <%--<asp:ListBox ID="ddllistProduct" runat="server" Width="250" Height="200" SelectionMode="Multiple"></asp:ListBox>--%>
                                    <asp:HiddenField ID="hidpID" runat="server" />
                                </td>
                                <td width="60px" valign="middle" align="center" style="border:none;">
                                    <input type="button" value=">>" id="btnChoice" style="margin-bottom:10px;padding:5px;" onclick="javascript:btnAddProduct();return false;" /><br />
                                    <input type="button" value="<<" id="btnUnChoice" style="padding:5px;" onclick="javascript:btnDelProduct();return false;" />
                                   <%-- <asp:Button ID="btnIn" runat="server" style="margin-bottom:10px;padding:5px;" Text=">>" /><br />
                                    <asp:Button ID="btnOut" runat="server" style="padding:5px;" Text="<<"/>--%>
                                </td>
                                <td valign="top" align="left" style="border:none;">
                                    <select id="listProductChoice" multiple="multiple" style="width:250px;height:200px;" ondblclick="javascript:dblDelProduct();"></select>
                                    <asp:HiddenField ID="hidIDproduct" runat="server" />
                                    <%--<asp:ListBox ID="ddllistProductChoice" runat="server" Width="250" Height="200" SelectionMode="Multiple"></asp:ListBox>--%>
                                </td>
                            </tr>
                            <tr>
                                <td align="left" colspan="3" style="border:none;">
                                    <asp:Label ID="lblError" runat="server" Text=""></asp:Label>
                                    <%--<span id="btnTest" onclick="javascript:btnTest(); return false;">Test</span>--%>
                                    </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </tbody>
        </table>
<%--    </ContentTemplate>
</asp:UpdatePanel>--%>